﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using QuanLyKho.DataAccess;
using QuanLyKho.Common;
using QuanLyKho.GUI.Library;
using QuanLyKho.GUI.Frm;

namespace QuanLyKho.GUI
{
    public partial class ucListAccount : UserControl
    {
        private DataSet dsTemp = new DataSet();
        private string NavClicked = "";
        private int totalRows = 25; //Tong so danh sach muon hien thi len DataGridView
        static public long ID = 0;
        private string strUserName = string.Empty;
        private enum NavButton
        {
            First = 1,
            Next = 2,
            Previous = 3,
            Last = 4,
        }

        public ucListAccount()
        {
            InitializeComponent();
            strUserName = string.Empty;
            binDataListAccount(strUserName);
        }

        private void binDataListAccount(string _UserName)
        {
            List<clsUser> listUser = new clsDataAccessUser().UserSelectAllWithName(_UserName);
            DataTable dt = new DataTable("User");

            dt.Columns.Add("Check", typeof(CheckBox));
            dt.Columns.Add("ID", typeof(long));
            dt.Columns.Add("STT", typeof(int));
            dt.Columns.Add("Tên tài khoản", typeof(string));
            dt.Columns.Add("Mật khẩu", typeof(string));
            dt.Columns.Add("Họ tên", typeof(string));
            dt.Columns.Add("Chức vụ", typeof(string));
            dt.Columns.Add("Admin", typeof(bool));
            dt.Columns.Add("Update", typeof(bool));
            dt.Columns.Add("Read", typeof(bool));
            dt.Columns.Add("Tạo lúc", typeof(string));


            DataRow dr;
            int stt = 0;

            foreach (clsUser obj in listUser)
            {
                dr = dt.NewRow();
                stt = stt + 1;
                dr[0] = (CheckBox)new CheckBox();
                dr[1] = obj.ID;
                dr[2] = stt;
                dr[3] = obj.UserName;
                dr[4] = obj.Password;
                dr[5] = obj.Fullname;
                dr[6] = obj.Position;
                dr[7] = obj.Admin;
                dr[8] = obj.Update;
                dr[9] = obj.Read;
                dr[10] = obj.Created.ToString("dd/MM/yyyy H:m:s");


                dt.Rows.Add(dr);
            }
            DataSet ds = new DataSet();
            ds.Tables.Add(dt);

            dsTemp = ds;
            if (ds.Tables[0].Rows.Count > 0)
                new clsPaging().fillDataGrid(ds, bindingSource1, dgvListAccount, "", lblPaging, totalRows);
            else
            {
                bindingSource1.DataSource = null;
                bindingSource1.Clear();
                dgvListAccount.DataSource = bindingSource1;
            }

            if (dgvListAccount.ColumnCount > 0)
            {
                dgvListAccount.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
                dgvListAccount.Columns[1].Visible = false;
                dgvListAccount.Columns[2].Visible = false;
                dgvListAccount.Columns[3].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
                dgvListAccount.Columns[4].Visible = false;
                dgvListAccount.Columns[5].Visible = false;
                dgvListAccount.Columns[6].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
                dgvListAccount.Columns[7].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;

                dgvListAccount.Columns[8].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
                dgvListAccount.Columns[8].ReadOnly = true;
                dgvListAccount.Columns[9].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
                dgvListAccount.Columns[9].ReadOnly = true;
                dgvListAccount.Columns[10].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;

                dgvListAccount.Columns[10].ReadOnly = true;

            }
        }

        private void ShowFormAddAccount(long _ID)
        {
            frmAddAccount obj = null;
            if (_ID < 1)
                obj = new frmAddAccount();
            else
                obj = new frmAddAccount(_ID);
            obj.StartPosition = FormStartPosition.CenterParent;
            obj.ShowInTaskbar = false;
            obj.MaximizeBox = false;
            obj.MinimizeBox = false;
            obj.ShowDialog();
            binDataListAccount(strUserName);
        }

        private void tsbFirst_Click(object sender, EventArgs e)
        {
            NavClicked = NavButton.First.ToString();
            new clsPaging().fillDataGrid(dsTemp, bindingSource1, dgvListAccount, NavClicked, lblPaging, totalRows);
        }

        private void tsbPrevious_Click(object sender, EventArgs e)
        {
            NavClicked = NavButton.Previous.ToString();
            new clsPaging().fillDataGrid(dsTemp, bindingSource1, dgvListAccount, NavClicked, lblPaging, totalRows);
        }

        private void tsbNext_Click(object sender, EventArgs e)
        {
            NavClicked = NavButton.Next.ToString();
            new clsPaging().fillDataGrid(dsTemp, bindingSource1, dgvListAccount, NavClicked, lblPaging, totalRows);
        }

        private void tsbLast_Click(object sender, EventArgs e)
        {
            NavClicked = NavButton.Last.ToString();
            new clsPaging().fillDataGrid(dsTemp, bindingSource1, dgvListAccount, NavClicked, lblPaging, totalRows);
        }

        private void dgvListAccount_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex < 0)
                return;

            string _ID = dgvListAccount.Rows[e.RowIndex].Cells["ID"].Value.ToString();
            if (Int64.TryParse(_ID, out ID))
            {
                if (ID < 1)
                    return;



                if (e.ColumnIndex == dgvListAccount.ColumnCount)
                {
                    if (HavitelMessageBox.Show("Bạn có muốn sửa tài khoản này?", "Thông báo", HavitelMessageBox.HavitelMessageButton.OKCancel,HavitelMessageBox.HavitelMessageIcon.Question) == DialogResult.OK)
                    {
                        ShowFormAddAccount(ID);
                    }
                }
            }
        }

        private void btnThemMoi_Click(object sender, EventArgs e)
        {
            ShowFormAddAccount(0);
        }

        private void txtAccName_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                string name = txtAccName.Text;
                binDataListAccount(name);
            }
        }

        private void cbostatus_CheckedChanged(object sender, EventArgs e)
        {
            if (chkCheckAll.Checked == true)
            {
                foreach (DataGridViewRow row in dgvListAccount.Rows)
                {
                    ((DataGridViewCheckBoxCell)row.Cells["Status"]).Value = true;
                }
            }
            else
            {
                foreach (DataGridViewRow row in dgvListAccount.Rows)
                {
                    ((DataGridViewCheckBoxCell)row.Cells["Status"]).Value = false;
                }
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            int i = 0;
            long ID;

            foreach (DataGridViewRow row in dgvListAccount.Rows)
            {
                if ((Convert.ToInt32(row.Cells["status"].Value) == 1) && (Convert.ToInt64(dgvListAccount.CurrentRow.Cells["status"].Value) == 1))
                    i++;

            }
            if (i > 1)
            {
                new clsLibrary().runLoading("Chỉ được phép sửa 1 tài khoản!", 0);

            }
            if (i == 1)
            {
                ID = Convert.ToInt64(dgvListAccount.CurrentRow.Cells["ID"].Value.ToString());
                ShowFormAddAccount(ID);
                new clsLibrary().runLoading("Đã sửa thành công!", 2);

            }
            if (i == 0)
            {
                new clsLibrary().runLoading("Chọn tài khoản muốn sửa!", 0);

            }
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            clsDataAccessUser listDataAccessListAccount = new clsDataAccessUser();
            if (Convert.ToInt64(dgvListAccount.CurrentRow.Cells["status"].Value) == 0)
                HavitelMessageBox.Show("Chọn tài khoản muốn xóa!", "Thông báo", HavitelMessageBox.HavitelMessageButton.OK, HavitelMessageBox.HavitelMessageIcon.Warning);
            else
            {
                if (HavitelMessageBox.Show("Bạn có muốn xóa thông tin tài khoản này không?", "Thông báo", HavitelMessageBox.HavitelMessageButton.OKCancel, HavitelMessageBox.HavitelMessageIcon.Question) == DialogResult.OK)
                {
                    foreach (DataGridViewRow row in dgvListAccount.Rows)
                    {
                        if (Convert.ToInt32(row.Cells["status"].Value) == 1)
                        {
                            long ID = Convert.ToInt64(row.Cells["ID"].Value.ToString());
                            listDataAccessListAccount.Delete(ID);
                            new clsLibrary().runLoading("Đã xóa tài khoản thành công!", 2);
                        }

                    }
                }
                else
                {
                    binDataListAccount(strUserName);
                }
            }
            binDataListAccount(strUserName);
        }
    }
}
